﻿using CQIE.JonJack.Models.Tabels;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CQIE.JonJack.Models.TabelConfig
{
    public class BookConfig : IEntityTypeConfiguration<Book>
    {
        public void Configure(EntityTypeBuilder<Book> builder)
        {
            // 设置主键
            builder.HasKey(b => b.Id);

            // 设置字段长度和是否必填
            builder.Property(b => b.BookName).IsRequired().HasMaxLength(100);
            builder.Property(b => b.Author).IsRequired().HasMaxLength(50);
            builder.Property(b => b.ISBN).IsRequired().HasMaxLength(20);
            builder.Property(b => b.Pubblish).HasMaxLength(50);
            builder.Property(b => b.BookClass).HasMaxLength(30);
            builder.Property(b => b.BookShelfNumber).HasMaxLength(10);

            // 配置价格为小数点后两位
            builder.Property(b => b.Price).HasColumnType("decimal(18,2)");

            // 配置一对多关系
            builder.HasMany(b => b.BookHistorys)
                   .WithOne(br => br.Book)
                   .HasForeignKey(br => br.BookId);

            // 配置一对多关系
            builder.HasMany(b => b.BorrowRecords)
                   .WithOne(br => br.Book)
                   .HasForeignKey(br => br.BookId);
        }
    }
}
